package com.baichang.taoshunda.dao.impl;

import com.baichang.framework.model.Page;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Repository;

import com.baichang.framework.dao.impl.BaseDaoImpl;
import com.baichang.taoshunda.dao.ScoreListDao;
import com.baichang.taoshunda.entity.ScoreList;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Repository
public class ScoreListDaoImpl extends BaseDaoImpl<ScoreList> implements ScoreListDao {

    /**
     * 兑换积分商品历史
     *
     * @param name
     * @param startCreate
     * @param account
     * @param nowPage
     * @param pageSize @return
     */
    @Override
    public Page queryHistoryGoodsScore(String name, String startCreate, String endCreate, String account, int nowPage, int pageSize) {
        List list = new ArrayList();
        String sql =" SELECT sl.*,sg.list_pic AS listPic,sg.score_goods_name AS goodsName,tu.nick_name AS userName,tu.account " +
                " FROM t_score_list sl INNER JOIN t_score_goods sg ON sl.convert_goods_id=sg.id INNER JOIN t_user tu ON sl.user_id = tu.id " +
                " WHERE score_state='-' ";
        if(StringUtils.isNotBlank(name)){
            sql +=" AND sg.score_goods_name LIKE ?";
            list.add("%"+name+"%");
        }
        if(!startCreate.equals("")&&!endCreate.equals("")){
            sql += " AND sl.created>=? AND sl.created <=DATE_ADD(?, Interval 1 DAY)";
            list.add(startCreate);
            list.add(endCreate);
        }
        if(StringUtils.isNotBlank(account)){
            sql +=" AND sl.address_user_phone LIKE ?";
            list.add("%"+account+"%");
        }
        sql += " ORDER BY sl.created DESC ";
        return pageForMapList(sql,nowPage,pageSize,list.toArray());
    }

    /**
     * 查询用户积分
     *
     * @param name
     * @param nowPage
     * @param pageSize
     * @return
     */
    @Override
    public Page<Map<String,Object>> queryUserScore(String name, int nowPage, int pageSize) {
        List list = new ArrayList();
        String sql = "SELECT sl.*,tu.nick_name AS nickName FROM t_score_list sl INNER JOIN t_user tu ON sl.user_id=tu.id WHERE sl.convert_goods_id IS NULL AND sl.convert_address IS NULL ";
        if(StringUtils.isNotBlank(name)){
            sql += " AND tu.nick_name LIKE ?";
            list.add("%"+name+"%");
        }
        sql += " ORDER BY sl.created DESC ";
        return pageForMapList(sql,nowPage,pageSize,list.toArray());
    }

    @Override
    public int getTodayCount(int userId) {
        String sql = "SELECT COUNT(1) FROM t_score_list WHERE user_id=? AND TO_DAYS(created)=TO_DAYS(NOW()) and  `source` LIKE '%签到%'";
        return queryForInt(sql, userId);
    }

    @Override
    public List<Map<String, Object>> convertList(Integer userId, Integer nowPage, Integer pageSize) {
        String sql = "SELECT score, sl.created, count, sg.list_pic listPic, sg.market_price marketPrice , sg.goods_pic as goodsPic, sg.score_goods_name as scoreGoodsName FROM t_score_list sl INNER JOIN t_score_goods sg ON sg.id = convert_goods_id WHERE sl.user_id = ? AND sl.source = '兑换积分商品'";
        return listForMapList(sql, nowPage, pageSize, userId);
    }

    /**
     * 送货状态
     * @param id
     * @param sendGoods
     * @return
     */
    @Override
    public int updateIsSendGoods(Integer id, Integer sendGoods) {
        String sql = "UPDATE t_score_list SET is_send_goods=? WHERE id=?";
        return update(sql, sendGoods, id);
    }
}